//
//  JTTimePicker.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/19.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "JTTimePicker.h"

@interface JTTimePicker ()

@property (nonatomic, strong) NSArray *minuteArr;

@end

@implementation JTTimePicker

+ (instancetype)sharedPicker {
    JTTimePicker *picker = [[JTTimePicker alloc] init];
    picker.datePickerMode = UIDatePickerModeTime;
    picker.minuteInterval = 15;
    picker.minimumDate = [NSDate date];
    
    NSDateFormatter *nowDateFormatter = [[NSDateFormatter alloc] init];
    [nowDateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *nowStr = [nowDateFormatter stringFromDate:[NSDate date]];
    
    NSDateFormatter *maxDateFormatter = [[NSDateFormatter alloc] init];
    [maxDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *maxDate = [maxDateFormatter dateFromString:[NSString stringWithFormat:@"%@ 22:00:00", nowStr]];
    
    picker.maximumDate = maxDate;
    
    return picker;
}

+ (instancetype)pickerWithDate:(NSDate *)date {
    JTTimePicker *picker = [[JTTimePicker alloc] init];
    picker.datePickerMode = UIDatePickerModeTime;
    picker.minuteInterval = 15;
    
    NSString *nowStr = [NSString string];
    if ([date compare:[NSDate date]] == NSOrderedDescending) {
        NSDateFormatter *nowDateFormatter = [[NSDateFormatter alloc] init];
        [nowDateFormatter setDateFormat:@"yyyy-MM-dd"];
        nowStr = [nowDateFormatter stringFromDate:date];
        
        NSDateFormatter *minDateFormatter = [[NSDateFormatter alloc] init];
        [minDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *minDate = [minDateFormatter dateFromString:[NSString stringWithFormat:@"%@ 06:00:00", nowStr]];
        
        picker.minimumDate = minDate;
    } else {
        //开始时间为选择器的最小时间
        NSDateFormatter *nowDateFormatter = [[NSDateFormatter alloc] init];
        [nowDateFormatter setDateFormat:@"yyyy-MM-dd"];
        nowStr = [nowDateFormatter stringFromDate:[NSDate date]];
        
        picker.minimumDate = [NSDate date];
    }
    
    NSDateFormatter *maxDateFormatter = [[NSDateFormatter alloc] init];
    [maxDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *maxDate = [maxDateFormatter dateFromString:[NSString stringWithFormat:@"%@ 22:00:00", nowStr]];
    
    picker.maximumDate = maxDate;
    
    return picker;
}

///**
// *  单例对象的全局变量
// */
//static JTTimePicker *_instance;
//
///**
// *  返回单例对象
// */
//+ (instancetype)sharedPicker {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance = [[self alloc] init];
////        _instance.delegate = _instance;
////        _instance.dataSource = _instance;
//        _instance.datePickerMode = UIDatePickerModeTime;
//        _instance.minuteInterval = 15;
//        _instance.minimumDate = [NSDate date];
//        
//        NSDateFormatter *nowDateFormatter = [[NSDateFormatter alloc] init];
//        [nowDateFormatter setDateFormat:@"yyyy-MM-dd"];
//        NSString *nowStr = [nowDateFormatter stringFromDate:[NSDate date]];
//        
//        NSDateFormatter *maxDateFormatter = [[NSDateFormatter alloc] init];
//        [maxDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//        NSDate *maxDate = [maxDateFormatter dateFromString:[NSString stringWithFormat:@"%@ 22:00:00", nowStr]];
//        
//        _instance.maximumDate = maxDate;
//        
//    });
//    return _instance;
//}
//
///**
// *  重写单例对象的alloc方法
// */
//+ (instancetype)allocWithZone:(struct _NSZone *)zone {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance = [super allocWithZone:zone];
//    });
//    return _instance;
//}
//
///**
// *  重写单例对象的copy方法
// */
//+ (instancetype)copyWithZone:(struct _NSZone *)zone {
//    return _instance;
//}

//- (NSArray *)minuteArr {
//    if (!_minuteArr) {
//        _minuteArr = @[@"00", @"15", @"30", @"45"];
//    }
//    return _minuteArr;
//}
//
//#pragma mark - 代理
//
//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
//    return 2;
//}
//
//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//    switch (component) {
//        case 0:
//            return 16;
//            break;
//        case 1:
//            return 4;
//            break;
//    }
//    return 0;
//}
//
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    switch (component) {
//        case 0: {
//            NSString *hour = [NSString stringWithFormat:@"%@", @(row + 6)];
//            return hour;
//        }
//            break;
//            
//        case 1: {
//            return self.minuteArr[row];
//        }
//    }
//    return 0;
//}
//
//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    if (component == 0) {
////        NSString *seletedCity = self.cityArray[row];
////        self.subLocalityArray = [self.cityDict objectForKey:seletedCity];
////        [_instance reloadComponent:1];
////        
////        NSInteger selectedCityIndex = [_instance selectedRowInComponent:1];
////        NSString *seletedSublocality = self.subLocalityArray[selectedCityIndex];
////        
////        NSString *msg = [NSString stringWithFormat:@"%@%@", seletedCity,seletedSublocality];
////        _instance.cityAndSubloca = msg;
//        _instance.time = [NSString stringWithFormat:@"%@时%@分", @(row + 6), self.minuteArr[[_instance selectedRowInComponent:1]]];
//    } else {
////        NSInteger selectedCityIndex = [_instance selectedRowInComponent:0];
////        NSString *seletedCity = self.cityArray[selectedCityIndex];
////        
////        NSString *seletedSublocality = self.subLocalityArray[row];
////        
////        NSString *msg = [NSString stringWithFormat:@"%@%@", seletedCity,seletedSublocality];
////        _instance.cityAndSubloca = msg;
//        _instance.time = [NSString stringWithFormat:@"%@时%@分", @([_instance selectedRowInComponent:0] + 6), self.minuteArr[row]];
//    }
//}

@end
